/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sifact.conexion;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author joselugb
 */
public class cConexion {
    static String usuario;//= "RCSG";//"CIPRO";
    static String password;//;= "SYSTEM"; //"GRUPO03";
    static String host = "localhost";//"192.168.1.3";
    static String puerto = "5432";
    static String sid = "SISFACINV";
    static String driver = "org.postgresql.Driver";
    static String urljdbc;
    
    public static void iniciar(String User,String pass) throws Exception{
        usuario = User;
        password = pass;
        //ulrjdbc = "jdbc:postgresql:" + usuario + "/" + password + "@" + host + ":" + puerto + ":" + sid;              
        urljdbc = "jdbc:postgresql:" + "//" + host + ":" + puerto + "/" + sid;
    }
    
    public  static Connection estado() throws Exception{
        Connection conector = null;
        try {
            Class.forName(driver).newInstance();
            //Hacemos la conexion
            conector = DriverManager.getConnection(urljdbc,usuario,password);
            //Aseguramos una correcta conexion a la BD postgreSQL
            //JOptionPane.showMessageDialog(null,"Consulta OK...!","Conexion Satisfactoria con la BD", JOptionPane.INFORMATION_MESSAGE);
            }
        catch(SQLException ex) {
            JOptionPane.showMessageDialog(null,"Error en consulta: "+ex,"Error en la Consulta a la BD", JOptionPane.ERROR_MESSAGE);
        }
        catch(ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null,"No hay conexion: "+ex,"Error Conexion a la BD", JOptionPane.ERROR_MESSAGE);            
	}
        return conector;

    }
    public static void cerrar() throws Exception{
        try{
            if(estado()!= null){
            estado().close();
        }
        }catch(ClassNotFoundException ex){
            JOptionPane.showMessageDialog(null,"Nunca hubo conexion: "+ex,"Error Conexion a la BD", JOptionPane.ERROR_MESSAGE);            
        }
        
    }
}
